<?php
// +----------------------------------------------------------------------
// | LTHINK [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.mymkj.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Taoge <liangtao.gz@foxmail.com>
// +----------------------------------------------------------------------
namespace app\index\controller;

use think\Controller;
use lt\ThinkSDK\ThinkOauth;
use app\index\event\TypeEvent;
use think\Db;
/**
 * 第三方登录控制器
 * 登录及回调的入口
 */
class Oauth extends Controller
{
    //登录地址
    public function login($type = null)
    {
        empty($type) && $this->error('参数错误');
        //加载ThinkOauth类并实例化一个对象
        $sns = ThinkOauth::getInstance($type);
        //跳转到授权页面
        $this->redirect($sns->getRequestCodeURL());
    }

    //授权回调地址
    public function callback($type = null, $code = null)
    {
        //支付宝code
        if ('alipay' == $type) {
            $code = $_GET['auth_code'];
        }
        (empty($type) || empty($code)) && $this->error('参数错误');

        //加载ThinkOauth类并实例化一个对象
        $sns = ThinkOauth::getInstance($type);


        //腾讯微博需传递的额外参数
        $extend = null;
        if ($type == 'tencent') {
            $extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey'));
        }

        //请妥善保管这里获取到的Token信息，方便以后API调用
        //调用方法，实例化SDK对象的时候直接作为构造函数的第二个参数传入
        //如： $qq = ThinkOauth::getInstance('qq', $token);
        $token = $sns->getAccessToken($code, $extend);
        $info = Db::table('zb_user.tp_sdk_login')->where('openid',$token['openid'])->find();
        if(!empty($info['user_id'])){
            $token = Db::table('zb_login.tp_token')->where('user_id',$info['user_id'])->find();
            $this->redirect('/dist/#/home?token='.$token['token']);exit;
        }
        //获取当前登录用户信息
        if (is_array($token)) {
            $TypeEven = new \app\index\event\TypeEvent();
            $result = $TypeEven->$type($token);

            if ($result) {
                $info = $this->save_data($result);

                $token['token'] = null;

//                $this->success("恭喜！使用{$type}用户登录成功！", url('index/index', ['status' => 1, 'sdk_login_id' => $info['sdk_login_id'], 'user_id' => $info['user_id']]));
                $this->redirect('/dist/#/home?token='.$token['token'].'&sdk_id='.$info['sdk_login_id']);
                //$this->redirect('center/index');
            } else {
                $this->error($result);
            }
        }
    }
    public function save_data($user_info){
        $user_info=array (
            'type' => 'QQ',
            'name' => '深圳掌邦科技',
            'nick' => '深圳掌邦科技',
            'head' => 'http://qzapp.qlogo.cn/qzapp/101496008/3922469857CEBD040A2F285D43D2C5A8/100',
            'openid' => '3922469857CEBD040A2F285D43D2C5A8',
        );
        $map_old['type']=$user_info['type'];
        $map_old['openid']=$user_info['openid'];
        $old=Db::name("sdk_login")->where($map_old)->find();
        if($old){
            Db::name("sdk_login")->where($map_old)->update($user_info);
        }else{
            Db::name("sdk_login")->insert($user_info);
        }

    }


}
